<?php
/**
 * 抽奖活动模型
 *
 *
 *
 *
 
 */
defined('InShopNC') or exit('Access Invalid!');
class lottery_activityModel extends Model{

    public function __construct() {
        parent::__construct('lottery_activity');
    }

	/**
     * 读取抢购列表
	 * @param array $condition 查询条件
	 * @param int $page 分页数
	 * @param string $order 排序
	 * @param string $field 所需字段
     * @return array 抢购列表
	 *
	 */
	public function getActivityList($condition, $page = null, $order = 'state asc', $field = '*', $limit = 0) {
        return $this->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
	}
	
	public function getActivityExtendList($condition, $page = null, $order = 'state asc', $field = '*', $limit = 0) {
	    $activity_list = $this->getActivityList($condition, $page, $order, $field, $limit);
	    if(!empty($activity_list)) {
	        for ($i=0,$j=count($activity_list); $i<$j; $i++) {
	            $activity_list[$i] = $this->getExtendInfo($activity_list[$i]);
	        }
	    }
	    return $activity_list;
	}
	
	public function getExtendInfo($activity_info) {
	    if(!empty($activity_info)) {
	        $activity_info['exchage_start_date_text'] = date('Y-m-d H:i', $activity_info['exchange_start_date']);
	        $activity_info['exchage_end_date_text'] = date('Y-m-d H:i', $activity_info['exchange_end_date']);
	    }
	    return $activity_info;
	}
	
	/**
	 * 根据条件读取活动信息
	 * @param array $condition 查询条件
	 * @return array 活动信息
	 *
	 */
	public function getActivityInfo($condition) {
	    $activity_info = $this->where($condition)->find();
	    if (empty($activity_info)) return array();
	    $activity_info = $this->getExtendInfo($activity_info);
	    return $activity_info;
	}
	
	/**
	 * 读取活动表信息
	 * @param array $condition 查询条件
	 * @return array 活动信息
	 *
	 */
	public function getPureActivityInfo($condition) {
	    $activity_info = $this->where($condition)->find();
	    if (empty($activity_info)) return array();
	   
	    return $activity_info;
	}
	
	/*
	 * 增加
	 * @param array $param
	 * @return bool
	 *
	 */
	public function addActivity($param){
	    return $this->insert($param);
	}
	
	/*
	 * 删除
	 * @param array $param
	 * @return bool
	 *
	 */
	public function delActivity($condition){
        return $this->where($condition)->delete();
	}
	
	/**
	 * 更新
	 * @param array $update
	 * @param int|array $activityid
	 * @return boolean|unknown
	 */
	public function editActivityById($update, $activityid) {
	    if (empty($activityid)) {
	        return true;
	    }
	    $condition['lottery_activity_id'] = $activityid;
	    $result = $this->table('lottery_activity')->where($condition)->update($update);
	    return $result;
	}

}
